﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HtmlAgilityPack;
using Doves.Crawlement;
using Doves.DataAccessLayer;
using Doves.BussinessLayer;
using Doves.DataAccessLayer.DAO;

namespace Doves.ConsoleTestProject
{
    class Program
    {
        static void Main(string[] args)
        {
            //TestLinkCrawler();
            //TestArticleCrawler();
            CrawleArticleFromLinkInDB();
            //CrawleLinkToDB();
        }


        public static void CrawleLinkToDB()
        {
            Zomby myZom = new Zomby();
            myZom.Id = 2;
            myZom.Url = "http://vn.vnexpress.net/gl/doi-song/";
            LinkCrawler ACrawler = new LinkCrawler(myZom);
            List<Link> LinkList = ACrawler.ScanArticleLinks();
            Console.WriteLine("Count: {0}", LinkList.Count);
            foreach (Link link in LinkList)
            {
                //Insert Link to database
                LinkBO LinkBo = new LinkBO();
                LinkBo.InsertLink(link);
            }
            Console.WriteLine("Finish him !");
            Console.ReadKey();
            return;
        }


        public static void TestLinkCrawler()
        {
            Zomby myZom = new Zomby();
            myZom.Id = 2;
            myZom.Url = "http://vn.vnexpress.net/gl/xa-hoi/";
            LinkCrawler ACrawler = new LinkCrawler(myZom);
            List<Link> LinkList = ACrawler.ScanArticleLinks();
            Console.WriteLine("Count: {0}", LinkList.Count);
            foreach (Link link in LinkList)
            {
                //Insert Link to database
                LinkBO LinkBo = new LinkBO();
                LinkBo.InsertLink(link);
                
                //Write to destop
                Console.WriteLine("================ obj ===================");
                Console.WriteLine(link.Url);
                Console.WriteLine(link.ThumbImage);
                Console.WriteLine();
            }
            Console.ReadKey();
            return;
        }

        private static void CrawleArticleFromLinkInDB()
        {
            ArticleCrawleJob ObjJob = new ArticleCrawleJob();
            ObjJob.CrawleArticles();
        }

        private static void TestArticleCrawler()
        {
            Link aLink = new Link();
            aLink.Url = @"http://vn.vnexpress.net/gl/the-gioi/tu-lieu/2012/10/thu-tinh-cua-bac-hy-lai/";
            aLink.ZomId = 2;

            ArticleCrawleJob ObjJob = new ArticleCrawleJob();
            ArticleCrawlerInputDataType inputData = ObjJob.PreparingCrawleDateFromDB(aLink);

            ArticleCrawler ArCrawlerObj = new ArticleCrawler(inputData);
            Article MyArticle = ArCrawlerObj.ScanArticleContent();

            if (MyArticle == null)
            {
                Console.WriteLine("Can't connect website");
            }
            else
            {
                Console.WriteLine("======= obj ============");
                Console.WriteLine(MyArticle.Title);
                Console.WriteLine(MyArticle.Description);
                Console.WriteLine(MyArticle.Body);
                Console.WriteLine(MyArticle.CreatedTime);
            }

            Console.ReadKey();
            return;
        }
    }
}
